package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Strategy;
import cn.wolfcode.domain.StrategyCatalog;
import cn.wolfcode.domain.StrategyTheme;
import cn.wolfcode.query.StrategyCatalogQuery;
import cn.wolfcode.repository.RegionRepository;
import cn.wolfcode.repository.StrategyCatalogRepository;
import cn.wolfcode.service.IStrategyCatalogService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.vo.CatalogVO;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void insert(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> listAll() {
        return strategyCatalogRepository.findAll();
    }

    @Override
    public Page query(StrategyCatalogQuery qo) {
        //创建查询对象
        Query query = new Query();
        //查询总条数
        long count = template.count(query, StrategyCatalog.class);
        if (count == 0){
            return Page.empty();
        }
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(page);
        List<StrategyCatalog> list = template.find(query, StrategyCatalog.class);
        PageImpl<StrategyCatalog> regions = new PageImpl<>(list, page, count);
        return regions;
    }

    @Override
    public List<CatalogVO> queryList() {
        List<CatalogVO> list = new ArrayList<>();
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults();
        for (Map data : datas) {
            CatalogVO vo = new CatalogVO();
            //给vo对象设置destName字段
            vo.setDestName(data.get("_id").toString());
            //取出map集合里的names集合
            List<Object> names = (List<Object>) data.get("names");
            //取出id集合
            List<Object> ids = (List<Object>) data.get("ids");

            List<StrategyCatalog> catalogList = new ArrayList<>();
            for (int i=0;i<names.size();i++){
                //创建分类对象
                StrategyCatalog catalog = new StrategyCatalog();
                //遍历取出每个主题名称
                String name = names.get(i).toString();
                //遍历取出每个主题id
                String id = ids.get(i).toString();
                catalog.setId(id);
                catalog.setName(name);
                catalogList.add(catalog);
            }
            vo.setList(catalogList);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> findByDestId(String id) {
        List<StrategyCatalog> list = strategyCatalogRepository.findByDestId(id);
        for (StrategyCatalog strategyCatalog : list) {
            List<Strategy> sts = strategyService.findByDestId(strategyCatalog.getId());
            strategyCatalog.setStrategies(sts);
        }
        return list;
    }
}
